#!/bin/sh -e

## Barry is a GPL C++ library for interfacing with the RIM BlackBerry
## Handheld. It comes with a command line tool for exploring the device
## and a GUI for making quick backups. This project's goal is to create
## a fully functional syncing mechanism on Linux.
## http://sourceforge.net/projects/barry/
##
## Written by "Vincent Batts <vbatts@batts.mine.nu>"
##
## opensync consideration added thanks
##   to "Heinz Wiesinger <HMWiesinger@gmx.at>"

# Modified by Robby Workman <rworkman@slackbuilds.org>

PRGNAM=barry
VERSION=0.14
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# If you want to use a group other than "plugdev" for barry,
# specify it on the command line as BARRY_GROUP - for example:
#   BARRY_GROUP=barry ./barry.SlackBuild
BARRY_GROUP=${BARRY_GROUP:-plugdev}

# Enable opensync-plugin?
OPENSYNC=${OPENSYNC:-no}

if [ "$OPENSYNC" = "no" ]; then
  opensync_opt="dis"
else
  opensync_opt="en"
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R a-s,u+rw,go+r-w .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --disable-static \
  --${opensync_opt}able-opensync-plugin \
  --build=$ARCH-slackware-linux

make || exit 1
make install DESTDIR=$PKG || exit 1

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README TODO COPYING AUTHORS NEWS ChangeLog \
  $PKG/usr/doc/$PRGNAM-$VERSION

mkdir -p $PKG/lib/udev/rules.d/
sed s/SYSFS/ATTR/g udev/10-blackberry.rules > \
  $PKG/lib/udev/rules.d/10-blackberry.rules
sed -e s/SYSFS/ATTR/g udev/99-barry-perms > \
  $PKG/lib/udev/rules.d/99-barry-permissions.rules
if [ "$BARRY_GROUP" != "barry" ]; then
  sed -i "s/GROUP=\"barry\""/GROUP=\"$BARRY_GROUP\"/ \
    $PKG/lib/udev/rules.d/99-barry-permissions.rules
fi

mkdir -p $PKG/etc/modprobe.d
echo "blacklist berry-charge" > $PKG/etc/modprobe.d/barry

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-${BUILD}${TAG}.tgz
